package com.wqr.it.ecwdr.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.wqr.it.ecwdr.dao.ISallerDao;
import com.wqr.it.ecwdr.repository.SallerRepository;
import com.wqr.it.ecwdr.vo.SallerVO;

@CacheConfig(cacheNames = "saller")
@Service
public class SallerService {
	private static final Logger log = LoggerFactory.getLogger("INFO");
	@Autowired
	private ISallerDao sallerDao;
	@Autowired
	private SallerRepository sallerRepository;

	@Cacheable
	public SallerVO findBySn(String sn, int countryId, int sourceId) {
		Assert.notNull(sn, "Saller sn can not be empty!");
		SallerVO sallerVO = sallerDao.findSallerBySN(sn, countryId + "", sourceId + "");
		return sallerVO;
	}
	
	public SallerVO save(SallerVO sallerVO) {
		SallerVO s = null;
		try {
		s = sallerRepository.save(sallerVO);
		} catch (Exception e) {
			log.error(sallerVO.toString() + "\n" + e.getMessage());
			e.printStackTrace();
			throw e;
		}
		return s;		
	}
}
